package com.lzy.global.exception.handler;

import com.lzy.constant.ResultCodeEnum;
import com.lzy.global.exception.GlobalBusinessException;
import com.lzy.model.RestResponseResult;
import com.lzy.util.ResultThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.EOFException;
import java.io.FileNotFoundException;
import java.sql.SQLException;

/**
 * @author lzy
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 运行异常处理
     */
    @ExceptionHandler(value = RuntimeException.class)
    @ResponseBody
    public RestResponseResult runtimeExceptionErrorHandle(RuntimeException e) {
        log.error("RuntimeException:", e);
        RestResponseResult response = RestResponseResult.failResponse(ResultCodeEnum.服务器异常.getCode(), e.getMessage());
        ResultThreadLocalUtil.setRestResponse(response);
        return response;
    }

    /**
     * 空指针异常处理
     */
    @ExceptionHandler(value = NullPointerException.class)
    @ResponseBody
    public RestResponseResult nullPointerExceptionErrorHandle(NullPointerException e) {
        log.error("NullPointerException:", e);
        RestResponseResult response = RestResponseResult.failResponse(ResultCodeEnum.服务器异常.getCode(), "网络繁忙,请稍后再试。。。");
        ResultThreadLocalUtil.setRestResponse(response);
        return response;
    }

    /**
     * MVC参数异常
     */
    @ExceptionHandler(value = MissingServletRequestParameterException.class)
    @ResponseBody
    public RestResponseResult missingServletRequestParameterExceptionErrorHandle(MissingServletRequestParameterException e) {
        log.error("MissingServletRequestParameterException", e);
        RestResponseResult response = RestResponseResult.failResponse(ResultCodeEnum.服务器异常.getCode(), "参数异常");
        ResultThreadLocalUtil.setRestResponse(response);
        return response;
    }

    /**
     * MVC参数异常
     */
    @ExceptionHandler(value = IllegalArgumentException.class)
    @ResponseBody
    public RestResponseResult illegalArgumentExceptionErrorHandle(IllegalArgumentException e) {
        log.error("IllegalArgumentException", e);
        RestResponseResult response = RestResponseResult.failResponse(ResultCodeEnum.服务器异常.getCode(), "参数异常");
        ResultThreadLocalUtil.setRestResponse(response);
        return response;
    }

    /**
     * 运算异常
     */
    @ExceptionHandler(value = ArithmeticException.class)
    @ResponseBody
    public RestResponseResult arithmeticExceptionErrorHandle(ArithmeticException e) {
        log.error("ArithmeticException", e);
        RestResponseResult response = RestResponseResult.failResponse(ResultCodeEnum.服务器异常.getCode(), "网络繁忙,请稍后再试。。。");
        ResultThreadLocalUtil.setRestResponse(response);
        return response;
    }


    /**
     * 对象转换失败
     */
    @ExceptionHandler(value = ClassCastException.class)
    @ResponseBody
    public RestResponseResult classCastExceptionErrorHandle(ClassCastException e) {
        log.error("ArithmeticException", e);
        RestResponseResult response = RestResponseResult.failResponse(ResultCodeEnum.服务器异常.getCode(), "网络繁忙,请稍后再试。。。");
        ResultThreadLocalUtil.setRestResponse(response);
        return response;
    }

    /**
     * 文件异常(未找到或不存在)
     */
    @ExceptionHandler(value = FileNotFoundException.class)
    @ResponseBody
    public RestResponseResult fileNotFoundExceptionErrorHandle(FileNotFoundException e) {
        log.error("FileNotFoundException", e);
        RestResponseResult response = RestResponseResult.failResponse(ResultCodeEnum.服务器异常.getCode(), "网络繁忙,请稍后再试。。。");
        ResultThreadLocalUtil.setRestResponse(response);
        return response;
    }

    /**
     * 反射调用时异常
     */
    @ExceptionHandler(value = ReflectiveOperationException.class)
    @ResponseBody
    public RestResponseResult reflectiveOperationExceptionErrorHandle(ReflectiveOperationException e) {
        log.error("ReflectiveOperationException", e);
        RestResponseResult response = RestResponseResult.failResponse(ResultCodeEnum.服务器异常.getCode(), "网络繁忙,请稍后再试。。。");
        ResultThreadLocalUtil.setRestResponse(response);
        return response;
    }

    /**
     * 文件已结束异常
     */
    @ExceptionHandler(value = EOFException.class)
    @ResponseBody
    public RestResponseResult eOFExceptionErrorHandle(EOFException e) {
        log.error("EOFException", e);
        RestResponseResult response = RestResponseResult.failResponse(ResultCodeEnum.服务器异常.getCode(), "网络繁忙,请稍后再试。。。");
        ResultThreadLocalUtil.setRestResponse(response);
        return response;
    }

    @ExceptionHandler(value = SQLException.class)
    @ResponseBody
    public RestResponseResult sQLExceptionErrorHandle(SQLException e) {
        log.error("SQLException", e);
        RestResponseResult response = RestResponseResult.failResponse(ResultCodeEnum.数据库操作异常.getCode(), "数据服务异常");
        ResultThreadLocalUtil.setRestResponse(response);
        return response;
    }

    @ExceptionHandler(value = GlobalBusinessException.class)
    @ResponseBody
    public RestResponseResult globalBusinessExceptionErrorHandle(GlobalBusinessException e) {
        log.error("GlobalBusinessException", e);
        RestResponseResult response = RestResponseResult.failResponse(e.getCode(), e.getText());
        ResultThreadLocalUtil.setRestResponse(response);
        return response;
    }

    /**
     * 全局异常处理
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public RestResponseResult defaultErrorHandler(Exception e) {
        log.error("Exception", e);
        RestResponseResult response = RestResponseResult.failResponse(ResultCodeEnum.服务器异常.getCode(), "网络繁忙,请稍后再试。。。");
        ResultThreadLocalUtil.setRestResponse(response);
        return response;
    }


}